cap program drop frolichdgp
program define frolichdgp
syntax, n(integer) design(integer) var(real) curves(numlist) 
qui{
	drop _all
	set obs `n'
** Selection Equation
    gen design = `design'
	if `design'==1 matrix kappa = (0.00,1.00)
	if `design'==2 matrix kappa = (0.15,0.70)
	if `design'==3 matrix kappa = (0.30,0.40)
	if `design'==4 matrix kappa = (0.00,0.40)
	if `design'==5 matrix kappa = (0.60,0.40)
	gen Z1 = rnormal()*sqrt(2)
	gen X = exp(Z1)/(1+exp(Z1))
	gen Xb = el(kappa,1,1)+el(kappa,1,2)*X
	gen u = -runiform()
	gen pX = Xb
	replace pX=0 if pX<0
	replace pX=1 if pX>1
	gen Tstar= Xb + u
	gen T= Tstar>=0
** Outcome Equation
    gen var = `var'
	gen e =(runiform()-.5)*sqrt(var)*sqrt(12)
	local X = "X"
	foreach k of local curves{
		if `k'==1 gen m1 = 0.15 + 0.7* `X'                                     
		if `k'==2 gen m2 = 0.1 + `X'/2 + (1/2)*exp(-200*(`X'-0.7)^2)           
		if `k'==3 gen m3 = 0.8 - 2*(`X'-0.9)^2 - 5*(`X'-0.7)^3 - 10*(`X'-0.6)^10 
		if `k'==4 gen m4 = 0.2 + sqrt(1-`X') - 0.6*(0.9-`X')^2                  
		if `k'==5 gen m5 = 0.2 + sqrt(1-`X') - 0.6*(0.9-`X')^2- 0.1*`X'*cos(30*`X')
		if `k'==6 gen m6 = 0.4 + 0.25*sin(8*`X'-5) + 0.4*exp(-16*(4*`X'-2.5)^2)
		gen y`k' = 0*T + m`k' + e
		}
** Organize
*drop  e u Tstar
gen Z2 = Z1^2
gen Z3 = Z1^3
gen Z4 = Z1^4
aorder

}
end

